package com.gexiaobao.pigeon.viewmodel

import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.app.model.bean.DeviceInfoResponse
import com.gexiaobao.pigeon.app.model.bean.DeviceListBean
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.model.param.AddDeviceParam
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.state.ResultState

/**
 * created by : hxw
 * @date : 20221122
 * Describe :
 */
class MyDeviceViewModel : BaseViewModel() {

    private var pageNo: Int = 1
    val deviceCode = StringObservableField()

    /** ---------------------设备详情--------------------------- */
    val deviceType = StringObservableField()
    val pigeonHouseName = StringObservableField()
    val deviceLongitude = StringObservableField()
    val deviceLatitude = StringObservableField()
    val deviceCommStatus = StringObservableField()
    val deviceScanStatus = StringObservableField()
    val deviceTBStatus = StringObservableField()
    val deviceTBStatus1 = StringObservableField()
    val deviceTBStatus2 = StringObservableField()
    val deviceGPSStatus = StringObservableField()
    val devicePowerStatus = StringObservableField()
    val deviceCameraStatus = StringObservableField()
    val deviceDays = StringObservableField()


    /** 添加设备 */
    val addPigeonDevice: MutableLiveData<UiState> = MutableLiveData()

    /** 删除设备 */
    val deletePigeonDevice: MutableLiveData<UiState> = MutableLiveData()

    /** 修改设备 */
    val upDatePigeonDevice: MutableLiveData<UiState> = MutableLiveData()

    /** 设备详情信息 */
    val deviceDetailInfo = MutableLiveData<ResultState<DeviceInfoResponse>>()

    /**  设备列表  */
    var deviceList: MutableLiveData<DeviceListBean> = MutableLiveData()

    fun getDeviceList() {
        request({ apiService.getDevice() }, {
            deviceList.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    fun createPigeonDevice(body: AddDeviceParam) {
        request({ apiService.createPigeonDevice(body) }, {
            val addDevice = UiState(isSuccess = true)
            addPigeonDevice.value = addDevice
        }, {
            val addDevice = UiState(isSuccess = false, errorMsg = it.msg)
            addPigeonDevice.value = addDevice
        })
    }

    fun deletePigeonDevice(id: Int) {
        request({ apiService.deletePigeonDevice(id) }, {
            val addDevice = UiState(isSuccess = true)
            deletePigeonDevice.value = addDevice
        }, {
            val addDevice = UiState(isSuccess = false, errorMsg = it.msg)
            deletePigeonDevice.value = addDevice
        })
    }

    fun updatePigeonDevice(body: AddDeviceParam) {
        request({ apiService.updatePigeonDevice(body) }, {
            val addDevice = UiState(isSuccess = true)
            upDatePigeonDevice.value = addDevice
        }, {
            val addDevice = UiState(isSuccess = false, errorMsg = it.msg)
            upDatePigeonDevice.value = addDevice
        })
    }

    fun getPigeonDeviceInfo(id: Int) {
        request({ apiService.getPigeonDeviceInfo(id) }, deviceDetailInfo)
    }
}